package cn.com.controller;

import cn.com.dao.TeacherDao;
import cn.com.entity.MyClass;
import cn.com.entity.TeacherScore;
import cn.com.entity.User;
import cn.com.tool.Jdbc;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>Project:Academic - TeaController
 *
 * @author 黄俊玮 On Administrator
 * @version 1.0
 * @DataTime 2024/6/14 08:49:42
 * @since 1.8
 */
@SuppressWarnings({"all"})
public class TeaController {
    private TeacherDao teacherDao = new TeacherDao();

    public void doHome_one(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/jsp/tea/update.jsp").forward(request,response);
    }
    public void modifyNow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        int phone = Integer.parseInt(request.getParameter("phone"));
        User oleId = (User) request.getSession().getAttribute("loginState");
        if (teacherDao.show(name, pwd,phone,oleId.getId())) {
            if (!name.equals(oleId.getName()))oleId.setName(name);//改变session数据
            if (!pwd.equals(oleId.getPwd()))oleId.setPwd(pwd);//改变session数据
            if (!(phone == oleId.getPhone()))oleId.setPhone(phone);//改变session数据
            request.getSession().setAttribute("loginState",oleId);//改变session数据
            request.setAttribute("update","<span style=\"color: red\">(修改成功)</span>");
            request.getRequestDispatcher("/index.do").forward(request,response);
        }else {
            request.setAttribute("update","<span style=\"color: red\">(数据异常,或手机号已存在)</span>");
            request.getRequestDispatcher("/index.do").forward(request,response);
        }
    }

    public void doHome_two(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute("loginState");//取出id
        List<MyClass> list = teacherDao.find(user.getId());
        request.getSession().setAttribute("myclass",list);
        request.getRequestDispatcher("/WEB-INF/jsp/tea/find.jsp").forward(request,response);
    }
    public void Score(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String cid = request.getParameter("cid");
        String coid = request.getParameter("coid");

        List<TeacherScore> list = teacherDao.doFind(cid,coid);
        request.setAttribute("teaScore",list);
        request.setAttribute("cid",cid);
        request.setAttribute("coid",coid);
        request.getRequestDispatcher("/WEB-INF/jsp/tea/whole.jsp").forward(request,response);
    }
    public void updateScores(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        int sid = Integer.parseInt(req.getParameter("id"));
        String cid = req.getParameter("cid");
        int coid = Integer.parseInt(req.getParameter("coid"));
        double scores = Double.parseDouble(req.getParameter("scores"));


        if (!(scores>=0 && scores<=100)){
            req.setAttribute("whole","：<span style=\"color:red\">无效值</span>");
            req.getRequestDispatcher("/teacher/findClass/Score.do").forward(req,resp);
            return;
        }

        boolean key = teacherDao.modifyNow(sid,coid,scores);
        req.setAttribute("cid",cid);
        req.setAttribute("coid",coid);
        if (key){
            System.out.println("成功");
            req.setAttribute("whole","：<span style=\"color:red\">修改成功</span>");
            req.getRequestDispatcher("/teacher/findClass/Score.do").forward(req,resp);
        }else {
            System.out.println("失败");
            req.setAttribute("whole","：<span style=\"color:red\">修改失败</span>");
            req.getRequestDispatcher("/teacher/findClass/Score.do").forward(req,resp);
        }

    }
}
